package com.example.speedwaymanager;

import java.util.List;
import java.util.Vector;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class TableActivity extends FragmentActivity {
	
	public final static String EXTRA_CHANGE_MESSAGE = "com.example.myfirstapp.CHANGE_MESSAGE";
	private MyPageAdapterHeat mPagerAdapterHeat;
	private MyPageAdapterTable mPagerAdapterTable;
	private ViewPager mPager;
	private ViewPager mPager2;
	public TableActivity _context = this;
	CGameManager gm = CGameManager.getInstance();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_table);
		//Initialize the pager
		mPager = (ViewPager) findViewById(R.id.viewpager);
		mPager2 = (ViewPager) findViewById(R.id.viewpager2);		
		mPagerAdapterTable = new MyPageAdapterTable(getSupportFragmentManager(), this);
		mPager.setAdapter(mPagerAdapterTable);
		mPagerAdapterHeat = new MyPageAdapterHeat(getSupportFragmentManager(),this);
		mPager2.setAdapter(mPagerAdapterHeat);		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.table, menu);
		return true;
	}	
	
	public void startNextHeat(View view) {
		if ( !gm.get_match().isMatchCompleted() ) {
			gm.startNextHeat();
			mPagerAdapterTable.notifyDataSetChanged();
			mPagerAdapterHeat.notifyDataSetChanged();
			mPager2.setCurrentItem(gm.getCurrentHeatNo(), true);
			// check if change is possible - from heat 1
			if ( gm.get_match().get_teams().get(0).is_isUser() == true || gm.get_match().get_teams().get(1).is_isUser() ) {
				if ( gm.getCurrentHeatNo() == 1) {
					((Button) findViewById(R.id.btn_change)).setEnabled(true);				
				}
			}
			if ( gm.get_match().get_currentHeat() == 15) {
				//Button b = (Button) findViewById(R.id.button_startMatch);
				((TextView) view).setText("End Match");
			}			
		} else {
			this.finish();
		}		
	}
	
	public void changeTacticalClicked(View view) {
		// start change screen
		Intent intent = new Intent(this, ChangeActivity.class);
		intent.putExtra(EXTRA_CHANGE_MESSAGE, "TACTICAL");
		startActivity(intent);
	}
	
	public void changeClicked(View view) {
		// start change screen
		Intent intent = new Intent(this, ChangeActivity.class);
		intent.putExtra(EXTRA_CHANGE_MESSAGE, "CHANGE");
		startActivity(intent);
	}
	
	private void initialisePaging() {
		 CGameManager game = CGameManager.getInstance();
		 List<Fragment> fragmentsHeat = new Vector<Fragment>();
		 List<Fragment> fragmentsTable = new Vector<Fragment>();
		
		 for ( int i = 0 ; i < game.get_match().get_heats().size(); ++i )
		 {
			 Bundle args = new Bundle();
	         args.putInt("object", i+1);	       
	         fragmentsHeat.add(Fragment.instantiate(this, HeatFragment.class.getName(), args));
		 }
				 
     	Bundle args = new Bundle();
 		args.putInt("object", 1);
		fragmentsTable.add(Fragment.instantiate(this, TableFragment.class.getName(), args));
		Bundle args2 = new Bundle();
 		args2.putInt("object", 2);
		fragmentsTable.add(Fragment.instantiate(this, TableFragment.class.getName(), args2));		
	 }
	
	@Override
	protected void onResume() {
		super.onResume();
//		Toast.makeText(this, "onResume()" , Toast.LENGTH_SHORT).show();
		mPagerAdapterTable.notifyDataSetChanged();
		mPagerAdapterHeat.notifyDataSetChanged();
	}
}
